<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 */
if (!defined('IN_TINPHP')) {
	exit ('Access Denied');
}

/**
 * 每个 Logic 对应一个业务逻辑处理单元
 * 每个 Logic 必须继承自 BaseLogic
 * 每个 Logic 对应一个或多个 View/DAO
 */
class MenuLogic extends TinBaseLogic {

	private $menusDao;
	private $menusFunDao;

	// 每个 Logic 需要有一个构造函数
	public function MenuLogic() {
		parent::__construct();	
		$this->logger->className = "MenuLogic";
		$this->menusDao = Tin::getDAO("AssignTable","SYSMENU");
		$this->menusFunDao = Tin::getDAO("AssignTable","SYSMENU_FUNS");
	}
	
	public function __destruct(){
		$this->menusDao = null;
	}
	
	public function getMenuAll($cols, $cond, $order=null){
		return $this->menusDao->selectAll($cols, $cond, $order);
	}
	public function getMenuList($cols, $cond, $order=null, $page=null, $size=null, $bakcount=null){
        $res = array();
		$res['data'] = $this->menusDao->selectByCond($cols, $cond, $order, $page, $size);
		if($page && $bakcount){
			$res['count'] = $this->menusDao->getCountByCond($cond);
		}
		return $res;	
	}
	public function getMenuItem($cols, $cond){
		return $this->menusDao->selectRowByCond($cols, $cond);
	}
	
	public function getMenuFuncAll($cols, $cond, $order=null){
		return $this->menusFunDao->selectAll($cols, $cond, $order);		
	}
	public function getMenuFunItem($cols, $cond){
		return $this->menusFunDao->selectRowByCond($cols, $cond);
	}
	public function getAllMenuFun(){
		$permissList = $this->menusFunDao->selectAll();
		$rsMenus = array();
		$topMenu = $this->menusDao->selectAll('id','parent_id=0');
		
		foreach($topMenu as $k=>$v){
			if(!in_array($v['MENU_ID'],$rsMenus)){
				$rsMenus[] = $v['ID'];
			}
		}
		
		$res = array();
		$res['menuids'] = '';
		$res['functionids'] = '0';
		foreach($permissList as $k=>$v){
			if(!in_array($v['MENU_ID'],$rsMenus)){
				$rsMenus[] = $v['MENU_ID'];
			}
			if($v['ID'])
				$res['functionids'] .= ','.$v['ID'];
		}
		$res['menuids'] = implode(',', $rsMenus);
		return $res;
	}
	/** 添加菜单记录 */
	public function addMenuItem($data){
		$map = Tin :: createDataMap();
		$map->addEntry('TITLE', $data['title'], DB::VARCHAR);
		$map->addEntry('PARENT_ID', $data['parent_id'], DB::INT);
		$map->addEntry('URL', $data['url'], DB::VARCHAR);
		$map->addEntry('ORDER_NUM', $data['order_num'], DB::INT);
		$map->addEntry('STATE', $data['state'], DB::INT);
		$map->addEntry('CREATE_TIME', 'now()', DB::DATETIME);
		try{
			$res = $this->menusDao->insert($map, false, true);
			return $res;
		}catch(Exception $e){
            $this->logger->error("addRoleItem:".$e->getMessage());
			echo $e->getMessage();
			return null;
		}
	}
	/** 更新指定菜单记录 */
	public function updateMenuItem($data, $cond){
		$map = Tin :: createDataMap();
		$map->addEntry('TITLE', $data['title'], DB::VARCHAR);
		$map->addEntry('PARENT_ID', $data['parent_id'], DB::INT);
		$map->addEntry('URL', $data['url'], DB::VARCHAR);
		$map->addEntry('ORDER_NUM', $data['order_num'], DB::INT);
		$map->addEntry('STATE', $data['state'], DB::INT);
		try{
			$res = $this->menusDao->updateByCond($map, $cond, false);
			return $res;
		}catch(Exception $e){
            $this->logger->error("updateRoleItem".$e->getMessage());
			echo $e->getMessage();
			return null;
		}
	}
	/** 删除指定菜单记录 */
	public function delMenuItem($cond, $params = false){
		return $this->menusDao->deleteByCond($cond, $params);
	}
	/** 添加菜单记录 */
	public function addFunsItem($data){
		$map = Tin :: createDataMap();
		$map->addEntry('NAME', $data['name'], DB::VARCHAR);
		$map->addEntry('CTL', $data['ctl'], DB::VARCHAR);
		$map->addEntry('ACT', $data['act'], DB::VARCHAR);
		$map->addEntry('MENU_ID', $data['menu_id'], DB::INT);
		$map->addEntry('ORDER_NUM', $data['order_num'], DB::INT);
		try{
			$res = $this->menusFunDao->insert($map, false, true);
			return $res;
		}catch(Exception $e){
            $this->logger->error("addFunsItem:".$e->getMessage());
			echo $e->getMessage();
			return null;
		}
	}
	/** 更新指定菜单记录 */
	public function updateFunsItem($data, $cond){
		$map = Tin :: createDataMap();
		$map->addEntry('NAME', $data['name'], DB::VARCHAR);
		$map->addEntry('CTL', $data['ctl'], DB::VARCHAR);
		$map->addEntry('ACT', $data['act'], DB::VARCHAR);
		$map->addEntry('MENU_ID', $data['menu_id'], DB::INT);
		$map->addEntry('ORDER_NUM', $data['order_num'], DB::INT);
		try{
			$res = $this->menusFunDao->updateByCond($map, $cond, false);
			return $res;
		}catch(Exception $e){
            $this->logger->error("updateFunsItem".$e->getMessage());
			echo $e->getMessage();
			return null;
		}
	}
	/** 删除指定菜单记录 */
	public function delFunsItem($cond, $params = false){
		return $this->menusFunDao->deleteByCond($cond, $params);
	}
}//End Class
?>
